package com.crm.service.impl.sys;


import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.sys.UserAccountCustomMapper;
import com.crm.dao.mapper.sys.UserAccountMapper;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.UserAccountExample;
import com.crm.model.qo.sys.UserAccountQO;
import com.crm.model.vo.user.UserAccountManagerVO;
import com.crm.service.sys.UserAccountDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author luojie
 * @date 2020/9/23 15:20
 */
@Service
public class UserAccountDbServiceImpl implements UserAccountDbService {

    @Autowired
    UserAccountMapper userAccountMapper;

    @Autowired
    UserAccountCustomMapper userAccountCustomMapper;

    @Override
    public UserAccount selectUserAccountByAccountId(String accountId) {
        UserAccount userAccount = userAccountMapper.selectByPrimaryKey(accountId);
        return userAccount;
    }

    @Override
    public UserAccount selectUserAccountByUserAccount(String userAccount) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserAccountEqualTo(userAccount);
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        criteria.andAccountStatusEqualTo(Short.parseShort("0"));
        List<UserAccount> tbUserAccountList = userAccountMapper.selectByExample(userAccountExample);
        if(tbUserAccountList.size()>0){
            return tbUserAccountList.get(0);
        }else {
            return null;
        }

    }

    @Override
    public List<UserAccount> selectUserAccountListByDeptId(String deptId) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andDeptIdEqualTo(deptId);
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        List<UserAccount> accounts = userAccountMapper.selectByExample(userAccountExample);
        return accounts;
    }

    @Override
    public Boolean insertUserAccount(UserAccount userAccount) {
        int num = userAccountMapper.insertSelective(userAccount);
        return num > 0;
    }

    @Override
    public Boolean updateUserAccountByUserId(UserAccount userAccount) {
        int num = userAccountMapper.updateByPrimaryKey(userAccount);
        return num > 0;
    }

    /**
     * 获取权限组名单添加人员账号列表
     * @return
     */
    @Override
    public List<UserAccount> selectUserAccountList() {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        criteria.andUserIdNotEqualTo("0");
        List<UserAccount> accounts = userAccountMapper.selectByExample(userAccountExample);
        return accounts;
    }

    @Override
    public List<UserAccount> selectUserAccountByCompanyId(String companyId) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }

    @Override
    public List<UserAccount> selectUserAccountByCompanyIdExcludeLoginPerson(String companyId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andAccountIdNotEqualTo(userAccount.getAccountId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }

    /**
     * 根据公司id获取在职且未禁用账号列表
     * @Author renshaorong
     * @Date 2021/7/13
     */
    @Override
    public List<UserAccountManagerVO> getAbleAccountListByCompanyId(String companyId) {
        List<UserAccountManagerVO> userAccounts = userAccountCustomMapper.getAbleAccountListByCompanyId(companyId);
        return userAccounts;
    }

    @Override
    public List<UserAccount> getAllCoDeveloper() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andAccountIdNotEqualTo(userAccount.getAccountId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //账号状态启用
        //criteria.andAccountStatusEqualTo(Short.parseShort("0"));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }


    /**
     * 获取所有账号
     * @Author pym
     * @Date  2021/3/17 10:13 
     * @param 
     * @return {@link {java.util.List<com.crm.model.entity.sys.UserAccount>}}
     */
    @Override
    public List<UserAccount> getAllAccountList() {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }

    /**
     * @Description: 获取所有在职且未禁用账号列表
     * @Param: []
     * @Return: java.util.List<com.crm.model.entity.sys.UserAccount>
     * @Author: lixq-f
     * @Date: 2021/7/27
     **/
    @Override
    public List<UserAccountManagerVO> getAllUsedAccountList() {
        List<UserAccountManagerVO> userAccounts = userAccountCustomMapper.getAllUsedAccountList();
        return userAccounts;
    }

    /**
     * 根据账号ID查询用户ID
     * @param accountIds --账号ID集合
     * @return
     */
    @Override
    public List selectUserIdListByAccountIds(List<String> accountIds) {
        return userAccountCustomMapper.selectUserIdListByAccountIds(accountIds);
    }

    /**
     * 给定筛选条件查询用户信息列表（超级管理员）
     * @Author renshaorong
     * @Date 2021/4/7
     * @param userAccountQO 筛选条件
     * @param toString 排序
     * @return
     */
    @Override
    public List<UserAccountManagerVO> selectAllUserAccountList(UserAccountQO userAccountQO, String toString) {
        return userAccountCustomMapper.selectAllUserAccountList(userAccountQO,toString,true);
    }

    /**
     * 给定筛选条件查询用户信息列表（普通管理员）
     * @Author renshaorong
     * @Date 2021/4/7
     * @param userAccountQO 筛选条件
     * @param toString 排序
     * @return
     */
    @Override
    public List<UserAccountManagerVO> selectUserAccountListByCompanyId(UserAccountQO userAccountQO, String toString) {
        return userAccountCustomMapper.selectAllUserAccountList(userAccountQO,toString,false);
    }

    /**
     * 根据主键更新用户账号信息（字段为空时不更新）
     * @Author renshaorong
     * @Date 2021/4/8
     * @param userAccount
     * @return
     */
    @Override
    public Boolean updateUserAccountByPrimaryKeySelective(UserAccount userAccount) {
        int num = userAccountMapper.updateByPrimaryKeySelective(userAccount);
        return num > 0;
    }

    /***
     * @Description:根据账号名查询账号是否存在
     * @Param: accountName 账号名
     * @Author: luojie
     * @Date: 2021/5/19 11:33
     */
    @Override
    public Boolean selectUserAccountByAccountName(String accountName) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserAccountEqualTo(accountName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts.size()>0;
    }

    @Override
    public List<UserAccount> selectUserIdNotNull() {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserIdEqualTo("");
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }

    /**
     * 查询分机号是否存在
     * @Author pym
     * @Date  2021/6/9 9:02
     * @param extensionNumber 分机号
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean selectUserAccountByExtensionNumber(String extensionNumber) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andExtensionNumberEqualTo(extensionNumber);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts.size() > 0;
    }

    /**
     * 根据账号名查询账号信息
     * @Author pym
     * @Date  2021/6/10 11:04
     * @param accountName 账号名
     * @return {@link {com.crm.model.entity.sys.UserAccount}}
     */
    @Override
    public UserAccount selectUserAccountByName(String accountName) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserAccountEqualTo(accountName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        if (userAccounts != null && !userAccounts.isEmpty()) {
            return userAccounts.get(0);
        }
        return null;
    }

    /**
     * 根据用户名和公司id查询账号信息
     * @param createName
     * @param companyId
     * @return
     */
    @Override
    public UserAccountManagerVO selectUserAccountByNameAndCompanyId(String createName, String companyId) {
        return userAccountCustomMapper.selectUserAccountByNameAndCompanyId(createName,companyId);
    }

    /**
     * 根据用户名和账号查询账号是否存在
     * @param userName
     * @param userAccount
     * @return
     */
    @Override
    public UserAccountManagerVO selectUserAccountCount(String userName, String userAccount,String companyId) {
        return userAccountCustomMapper.selectUserAccountCount(userName,userAccount,companyId);
    }

    /**
     * 根据账号id物理删除
     * @param accountId
     */
    @Override
    public void deleteUserAccountById(String accountId) {
        //userAccountCustomMapper.deleteUserAccountById(accountId);
    }

    /**
     * 根据用户名查询账号信息
     * @Author pym
     * @Date  2021/6/10 11:04
     * @param AccountName 用户名
     * @return {@link {com.crm.model.entity.sys.UserAccount}}
     */
    @Override
    public UserAccount selectUserAccountByUserName(String AccountName) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserNameEqualTo(AccountName);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        if (userAccounts != null && !userAccounts.isEmpty()) {
            return userAccounts.get(0);
        }
        return null;
    }

    /**
     * 根据公司查询离职用户
     * @param companyId
     * @return
     */
    @Override
    public List<UserAccountManagerVO> selectLeaveUserAccountByCompanyId(String companyId) {
        return userAccountCustomMapper.selectLeaveUserAccountByCompanyId(companyId);
    }

    /**
     * 通过用户名和分公司id查询账户信息
     * @param accountName 用户名
     * @param companyId 分公司id
     * @return
     */
    @Override
    public UserAccount selectUserAccountByUserNameAndCompanyId(String accountName, String companyId) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserNameEqualTo(accountName);
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        if (userAccounts != null && !userAccounts.isEmpty()) {
            return userAccounts.get(0);
        }
        return null;
    }

    /**
     * 根据邮箱查询账号信息
     * @Author pym
     * @Date  2021/7/29 15:33
     * @param email 邮箱
     * @return {@link {com.crm.model.vo.user.UserAccountManagerVO}}
     */
    @Override
    public UserAccountManagerVO getUserAccountByEmail(String email) {
        List<UserAccountManagerVO> userAccounts = userAccountCustomMapper.getUserAccountByEmail(email);
        if (userAccounts != null && !userAccounts.isEmpty()) {
            return userAccounts.get(0);
        }
        return null;
    }

    @Override
    public UserAccount selectUserAccountByCompanyIdAndUserName(String companyId, String UserName) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andUserNameEqualTo(UserName);
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        if (userAccounts.size()>0) {
            return userAccounts.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<UserAccount> getAccountListPageByCompanyId(String companyId, String userName) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        if(!StringUtils.isBlank(userName)){
            criteria.andUserNameEqualTo(userName);
        }
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccounts = userAccountMapper.selectByExample(userAccountExample);
        return userAccounts;
    }

    @Override
    public UserAccount selectUserAccountByUserId(String userId) {
        UserAccountExample userAccountExample = new UserAccountExample();
        UserAccountExample.Criteria criteria = userAccountExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        //criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<UserAccount> userAccount = userAccountMapper.selectByExample(userAccountExample);
        if (Objects.nonNull(userAccount) && userAccount.size() > 0){
            return userAccount.get(0);
        }
        return new UserAccount();
    }

    /**
     * 查询用户
     * @return
     */
    @Override
    public List<UserAccountManagerVO> selectUserList(String companyId,String accountId) {
        return userAccountCustomMapper.selectUserList(companyId,accountId);
    }

}
